package com.yanjue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanjue.bo.UserBookshelfBO;
import com.yanjue.client.BookClient;
import com.yanjue.common.entity.Book;
import com.yanjue.common.entity.UserBookshelf;
import com.yanjue.common.util.Result;
import com.yanjue.common.util.ResultUtil;
import com.yanjue.common.vo.account.UserBookshelfVO;
import com.yanjue.mapper.UserBookshelfMapper;
import com.yanjue.service.UserBookshelfService;
import com.yanjue.service.task.UserBookshelfTask;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;


/**
* @author yanju
* @description 针对表【user_bookshelf(用户书架)】的数据库操作Service实现
* @createDate 2025-01-16 12:33:44
*/
@Service
public class UserBookshelfServiceImpl extends ServiceImpl<UserBookshelfMapper, UserBookshelf>
    implements UserBookshelfService {

    @Autowired
    private UserBookshelfMapper userBookshelfMapper;

    @Resource
    private ExecutorService userBookshelfQueueThreadPool;
    @Autowired
    private BookClient bookClient;


    /**
     * 同步书架
     * @param userId 用户id
     * @param bookshelfBO 业务书架
     */
    @Override
    public Result syncUserBookshelf(Integer userId, UserBookshelfBO bookshelfBO) {
        UserBookshelf bookshelf = new UserBookshelf();
        BeanUtils.copyProperties(bookshelfBO, bookshelf);
        bookshelf.setLastReadTime(System.currentTimeMillis());

        // 异步处理同步任务
        UserBookshelfTask task = new UserBookshelfTask(bookshelfBO.getSyncType(), bookshelf, this.userBookshelfMapper, userId);
        this.userBookshelfQueueThreadPool.execute(task);
        return ResultUtil.success();

    }

    /**
     * 获取用户书架
     * @param userId
     * @return
     */
    @Override
    public Result getUserBookshelf(Integer userId) {
        //获取书架结果
        LambdaQueryWrapper<UserBookshelf> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null){
            queryWrapper.eq(UserBookshelf::getUserId,userId);
        }
        //根据最后一次阅读时间降序
        queryWrapper.orderByDesc(UserBookshelf::getLastReadTime);
        //根据条件查找用户书架 虽然只有一个书架，用list保证扩展性
        List<UserBookshelf> userBookshelves = userBookshelfMapper.selectList(queryWrapper);
        List<UserBookshelfVO> bookshelfVOS = new ArrayList<>();
        for (UserBookshelf userBookshelf : userBookshelves) {
            Book book = bookClient.getBookById(userBookshelf.getBookId()).getData();
            if (book != null){
                UserBookshelfVO vo = new UserBookshelfVO();
                BeanUtils.copyProperties(userBookshelf,vo);
                vo.setAuthorName(book.getAuthorName());
                vo.setBookName(book.getBookName());
                vo.setImgUrl(book.getImgUrl());
                bookshelfVOS.add(vo);
            }

        }

        return ResultUtil.success(bookshelfVOS);
    }
}




